package com.cspg.ssss.util;

import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.config.RestTemplateConfig;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class HttpRequest {

    public static JSONObject httpGetRequest(String requestURL)
    {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60000);
        requestFactory.setReadTimeout(60000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        reInitMessageConverter(restTemplate);
        return  restTemplate.getForObject(requestURL, JSONObject.class);
    }

    public static JSONObject httpPostRequest(String requestURL,JSONObject data)
    {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60000);
        requestFactory.setReadTimeout(60000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        reInitMessageConverter(restTemplate);
        return  restTemplate.postForObject(requestURL,data,JSONObject.class);
    }

    /**
     * https get请求 跳过证书
     * @param requestURL
     * @return
     */
    public static JSONObject httpsGetRequest(String requestURL){
        RestTemplate restTemplateHttps = null;
        try {
            restTemplateHttps = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        reInitMessageConverter(restTemplateHttps);
        JSONObject retObj = restTemplateHttps.getForObject(requestURL,JSONObject.class);
        return retObj;
    }

    public static JSONObject httpsPostRequest(String requestURL,JSONObject data){
        RestTemplate restTemplateHttps = null;
        try {
            restTemplateHttps = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        reInitMessageConverter(restTemplateHttps);
        JSONObject retObj = restTemplateHttps.postForObject(requestURL,data,JSONObject.class);
        return retObj;
    }

    /*
     *初始化RestTemplate，RestTemplate会默认添加HttpMessageConverter
     * 添加的StringHttpMessageConverter非UTF-8
     * 所以先要移除原有的StringHttpMessageConverter，
     * 再添加一个字符集为UTF-8的StringHttpMessageConvert
     * */
    private static void reInitMessageConverter(RestTemplate restTemplate){
        List<HttpMessageConverter<?>> converterList=restTemplate.getMessageConverters();
        HttpMessageConverter<?> converterTarget = null;
        for (HttpMessageConverter<?> item : converterList) {
            if (item.getClass() == StringHttpMessageConverter.class) {
                converterTarget = item;
                break;
            }
        }

        if (converterTarget != null) {
            converterList.remove(converterTarget);
        }
        HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converterList.add(converter);
    }
}
